package com.chenfan.mcn.model.dto;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.annotation.JSONField;
import com.chenfan.mcn.clients.privilege.SUserVo;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.constant.SeparatorCst;
import com.chenfan.mcn.enums.StarTypeEnum;
import com.chenfan.mcn.model.StarBasicData;
import com.chenfan.mcn.utils.CommonUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
 * 导出红人信息批量更新-失败原因DTO
 * @author wulg
 * @date 2023/6/15
 */
@Data
public class StarBasicDataInfoOutputDTO extends StarBasicDataInfoInputDTO {

    private static final long serialVersionUID = 2291171473080067330L;

    /**
     * 失败原因
     */
    @Excel(name = "失败原因" , orderNum = "100")
    private String failedReason;

    @JSONField(serialize = false)
    private StarBasicData starBasicData;

    public void verifySysDataMatch(Map<String, StarBasicData> starMap, Map<String, SUserVo> userMapping, String separator) {
        if(StringUtils.isNotBlank(this.getStarCode())) {
            if(!starMap.containsKey(this.getStarCode())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "红人ID录入错误！"));
            } else {
                this.starBasicData = starMap.get(this.getStarCode());
            }
        }
        if(StringUtils.isNotBlank(this.getStarAgentName())) {
            if(!userMapping.containsKey(this.getStarAgentName())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "录入经纪人需要能跟EOP系统用户名匹配上！"));
            } else {
                this.setStarAgentId(userMapping.get(this.getStarAgentName()).getUserId());
            }
        }
        if(StringUtils.isNotBlank(this.getExecutorName())) {
            if(!userMapping.containsKey(this.getExecutorName())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "录入执行人需要能跟EOP系统用户名匹配上！"));
            } else {
                this.setExecutorId(userMapping.get(this.getExecutorName()).getUserId());
            }
        }
        if(Objects.isNull(starBasicData)) {
            return;
        }
        if(!StarTypeEnum.KOC.getCode().equals(starBasicData.getStarType())) {
            if(StringUtils.isBlank(this.getIdCardTypeName())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "证件类型必填！"));
            }
            if(StringUtils.isBlank(this.getIdCard())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "身份证号码必填！"));
            } else {
                if(NumberCst.ZERO.equals(this.getIdCardType()) && StringUtils.length(this.getIdCard()) != NumberCst.EIGHTEEN.intValue()) {
                    this.setFailedReason(CommonUtil.joinWith(SeparatorCst.LINE_BREAK,this.getFailedReason(), "身份证长度需录入18位！"));
                }
            }
            if(StringUtils.isBlank(this.getGenderName())) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "性别必填！"));
            }
        }

    }
}